package com.sicau.smart_farmer.controller;

import com.sicau.smart_farmer.entity.User;
import com.sicau.smart_farmer.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author sxt
 * @create 2019-11-20 1:10
 */
@Slf4j
@Controller
public class TestController {

    @RequestMapping("test")
    @ResponseBody
    public String test(HttpServletRequest request){
        String sessionId = request.getSession().getId();

        log.info(sessionId);
        return sessionId;
    }

    @RequestMapping("testThymeleaf")
    public ModelAndView testThymeleaf(){
        ModelAndView test = new ModelAndView("test");
        test.addObject("info",1);
        return test;
    }


    @RequestMapping(value = "/htmlorjson/{type}", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView ajaxPaymentBatchApproveProcess(@PathVariable String type)
    {
        if ("html".equals(type))
        {
            //index表示指定路径下的jsp页面的名称
            ModelAndView mav = new ModelAndView("test");
            mav.addObject("info", "hahaha");
            return mav;
        }
        else
        {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("info", "hahaha");
            return mav;
        }
    }



    @Autowired
    private UserService userService;

    @GetMapping("selectAllUserByPage")
    @ResponseBody
    public List<User> selectAllUserByPage(@RequestParam("page") Integer page, @RequestParam("limit")Integer pageNum){
        List<User> users = userService.selectAllUserByPage(page, pageNum);
        return users;

    }


}
